| 1 |  |  | import {each, noop, some} from './utils' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Install mixin onto the Vue instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @param {Vue} Vue - Vue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | export default function (Vue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 | 35 |  | 	const VueVersion = Number(Vue.version && Vue.version.split('.')[0]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 | 35 |  | 	const initHook = VueVersion && VueVersion > 1 ? 'beforeCreate' : 'init' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 | 35 |  | 	return { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 		[initHook]: beforeCreate(Vue), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 		created: created(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 		beforeDestroy: beforeDestroy(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		computed: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 			$loadingSyncers: loadingStateGetter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 		}, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		methods: { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 			$refreshSyncers: refreshSyncers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * Before creation hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @param {Vue} Vue - Vue | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | function beforeCreate(Vue) { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 35 |  | 	return function () { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 | 35 |  | 		this._syncers = {} | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 | 35 |  | 		const SyncCreator = Vue.$syncer.driver | 
            
                                                                        
                            
            
                                    
            
            
                | 35 | 35 |  | 		const synced = this.$options.sync | 
            
                                                                        
                            
            
                                    
            
            
                | 36 | 35 |  | 		if (synced) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 			// Set up each syncer | 
            
                                                                        
                            
            
                                    
            
            
                | 38 | 9 |  | 			each(synced, (settings, key) => { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 13 |  | 				this._syncers[key] = new SyncCreator(Vue, this, key, settings) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 13 |  | 				Object.defineProperty(this, key, { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 					get: () => { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 15 |  | 						return this._syncers[key] ? this._syncers[key].state : null | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 					}, | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  | 					set: noop, | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  | 					enumerable: true, | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  | 					configurable: true | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | 				}) | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 			}) | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 	} | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  * After creation hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | function created() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 35 |  | 	return function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		// Start syncers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 35 |  | 		each(this._syncers, syncer => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 13 |  | 			syncer.ready() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  * Before destruction hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | function beforeDestroy() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 35 |  | 	return function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 33 |  | 		each(this._syncers, (syncer, key) => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 11 |  | 			syncer.destroy() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 11 |  | 			delete this._syncers[key] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  * Get loading state of the syncers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  * @returns {boolean} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | function loadingStateGetter() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 9 |  | 	if (Object.keys(this._syncers).length > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 8 |  | 		return some(this._syncers, syncer => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 17 |  | 			return syncer.loading | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		}) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  | 	return false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  * Refresh syncers state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  * @param {string|string[]} [keys] - Syncers to refresh | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | function refreshSyncers(keys) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 4 |  | 	if (typeof keys === 'string') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 1 |  | 		keys = [keys] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 4 |  | 	if (!keys) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  | 		keys = Object.keys(this._syncers) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 4 |  | 	return Promise.all(keys.map(key => { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 6 |  | 		return this._syncers[key].refresh() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	})) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |